#!/bin/env python
# -*- coding: utf-8 -*-
import os
import json
import sys
from importlib import import_module
from record import *
from google.protobuf.json_format import MessageToDict

if '__main__' == __name__:
  print(sys.argv)
  if len(sys.argv) < 3:
    exit()
  path=sys.argv[1]
  requests=sys.argv[2]
  responses=sys.argv[3]
  requestType = getattr(import_module('proto.cn.migu.wheat.request.'+requests+'_pb2'),requests)
  responseType = getattr(import_module('proto.cn.migu.wheat.response.'+responses+'_pb2'),responses)
  r = RecordManager(path)
  for slice in r.snapshot():
    slice.with_detail()
    for x in slice:
      x.with_type(requestType, responseType)
      request = MessageToDict(x.request)
      response = MessageToDict(x.response)
      latency=x.message.responseTime-x.message.requestTime
      dict = {'request': request, 'response': response, 'requestTime': x.message.requestTime,'latency':latency,'request_id':x.request_id,'service':x.message.service+'/'+x.message.method}
      print(json.dumps(dict))


#cd /home/newarch/subscribe-service/whiteLog
#protoc proto/cn/migu/wheat/domain/* --python_out=./
#protoc proto/cn/migu/wheat/request/* --python_out=./
#protoc proto/cn/migu/wheat/request/* --python_out=./

#python3 example.py ../deal.ability.ChargeInfoService/getMonthPageInfo GetMonthPageInfoRequest GetMonthPageInfoResponse